package br.com.androidblog;

import java.util.ArrayList;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import br.com.androidblog.bean.ItemBlog;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListaBlog extends ListActivity {


	private String campoPesquisaNome;
	private String campoPesquisaCodigo;
	public ArrayList<String> listablog;

	public ArrayList<ItemBlog> listaBlogObjeto = new ArrayList<ItemBlog>();

	public void popularObjetos(Cursor c) {
		ItemBlog blog = new ItemBlog();
		blog.setCodItemBlog(Integer.parseInt(c.getString(0)));
		blog.setTituloBlog(c.getString(1));
		blog.setUrlBlog(c.getString(2));
		blog.setConteudo(c.getString(3));
		this.listaBlogObjeto.add(blog);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.listablog);

		setListAdapter(new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, getListaItensBlog()));
		

		LinearLayout layout = (LinearLayout) findViewById(R.id.publicidadeListaBlog);

		AdView adView = new AdView(this, AdSize.BANNER, "a14de7d2c14453a");

		layout.addView(adView);

		AdRequest request = new AdRequest();

		adView.loadAd(request);


	}


	private void abrirNoticia(int posicao) {
		Bundle b = new Bundle();
		ItemBlog c = listaBlogObjeto.get(posicao);
		b.putString("codItemBlog", String.valueOf(c.getCodItemBlog()));
		b.putString("conteudo", String.valueOf(c.getConteudo()));
		b.putString("titulo", String.valueOf(c.getTituloBlog()));
		b.putString("url", String.valueOf(c.getUrlBlog()));
		Intent mainIntent = new Intent(this, itemBlog.class);
		mainIntent.putExtras(b);
		startActivity(mainIntent);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		abrirNoticia(position);
	}

	private ArrayList<String> getListaItensBlog() {
		Log.i("agenda", "retornar lista de contatos");
		CursoModel cm = new CursoModel(getApplicationContext());
		
		Cursor c = cm.search(getApplicationContext(), "blog", new String[]{"_id", "titulo", "url", "conteudo"}, "titulo");
		
		this.listablog = new ArrayList<String>();
		
		if (c.moveToFirst()) {
			do {
				popularObjetos(c);
				this.listablog.add(c.getString(1));
			} while (c.moveToNext());
		}
		c.close();
		Log.i("agenda", "Quantidade de linhas no ArrayList  + " + this.listablog.size());
		
		if (this.listablog.size() == 0){
			
			this.listablog.add("Nenhum contato localizado");
			
		}
		c.close();
		cm.getConnection().close();
		return this.listablog;
	}
}
